﻿namespace Guia.Elements
{
    using System.Windows.Automation;
    using Guia.Elements.PatternServants;
    
    /// <summary>
    /// This class represents a particular row within a DataGrid.
    /// </summary>
    public abstract class AbstractDataGridRow : UiElement, ISelectionItemSingle
    {
        /// <summary>
        /// Represents a helper-servant for the SelectionItem-pattern.
        /// </summary>
        private readonly ISelectionItemMulti selectionItemServant;

        /// <summary>
        /// Initializes a new instance of the AbstractDataGridRow class. 
        /// </summary>
        /// <param name="automationElement">The AutomationElement representating the DataGridRow.</param>
        protected AbstractDataGridRow(AutomationElement automationElement)
            : base(automationElement)
        {
            this.selectionItemServant = new SelectionItemImpl(this);
        }

        /// <summary>
        /// Gets or sets a value indicating whether this DataGridRow is selected or not.
        /// </summary>
        public bool IsSelected
        {
            get
            {
                return this.selectionItemServant.IsSelected;
            }

            set
            {
                this.selectionItemServant.IsSelected = value;
            }
        }

        /// <summary>
        /// Selects the DataGridRow.
        /// If multiple selection is allowed, this selection causes that only this DataGridRow is selected. Thus, other DataGridRow may be unselected.
        /// </summary>
        public void Select()
        {
            this.selectionItemServant.Select();
        }

        /// <summary>
        /// Unselects the DataGridRow.
        /// </summary>
        public void Unselect()
        {
            this.selectionItemServant.Unselect();
        }

        /// <summary>
        /// Gets a list of the avaiable cells.
        /// </summary>
        /// <typeparam name="T">The concrete type of the cell.</typeparam>
        /// <returns>All typed cells.</returns>
        protected T[] GetCells<T>() where T : AbstractDataGridCell
        {
            return this.GetAll<T>();
        }
    }
}
